package org.darkforest.merchant.controller;

import org.darkforest.common.util.DateUtil;
import org.darkforest.controller.WebController;
import org.darkforest.dao.mapper.ComplainTalkMapper;
import org.darkforest.model.AlbumPic;
import org.darkforest.model.ComplainSearchBean;
import org.darkforest.model.ComplainTalk;
import org.darkforest.model.ComplainVo;
import org.darkforest.ucenter.auth.AuthPassport;
import org.darkforest.ucenter.service.ComplainService;
import org.darkforest.ucenter.service.UserOrderService;
import org.darkforest.vo.MemberBase;
import org.darkforest.vo.StoreSellerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by pc on 2016/1/14.
 */
@RequestMapping(value = "customer")
@Controller
public class ComplainContorller extends WebController<ComplainContorller> {
    @Autowired
    private ComplainService complaintService;
    @Autowired
    private ComplainTalkMapper complainTalkMapper;
    @Autowired
    private UserOrderService orderService;
    public static final String picTempPath = "ucenterTemp";

    @RequestMapping("complainList")
    @AuthPassport
    public ModelAndView complainList(ComplainSearchBean page,HttpServletRequest request) {
        ModelAndView view = new ModelAndView("complain/complainList");
        StoreSellerVo sellerVo= (StoreSellerVo)request.getSession().getAttribute("sellerInfo");
        page.setComplainActive(2);
        page.setStoreId(sellerVo.getStoreId());
        page.setResultList(complaintService.selectComplainForMerchant(page));
        view.addObject("page", page);
        return view;
    }
    @RequestMapping("complaininfo")
    public ModelAndView complaininfo(Integer complainId) {
        ComplainVo complainVo = complaintService.selectByPrimaryKey(complainId);
        ModelAndView view = new ModelAndView("complain/complaininfo","complain",complainVo);
        view.addObject("info",orderService.getOrderDetailById(complainVo.getOrderId(), complainVo.getAccuserId()));
        view.addObject("complainTalk", complainTalkMapper.selectByCondition(new ComplainTalk())) ;
        return view;
    }
    @RequestMapping(value="getcomplaintalkList")
    public  @ResponseBody
    List<ComplainTalk> complaintalkList(ComplainTalk complainTalk) throws Exception {
        return complainTalkMapper.selectByCondition(complainTalk);
    }

    @RequestMapping(value="publishComplainTalk")
    public  @ResponseBody String  publishComplainTalk(ComplainTalk complainTalk,HttpServletRequest request) throws Exception {
        StoreSellerVo sellerVo= (StoreSellerVo)request.getSession().getAttribute("sellerInfo");
        complainTalk.setTalkDatetime(DateUtil.unixTimeStampInt().intValue());
        complainTalk.setTalkMemberName(sellerVo.getMemberName());
        complainTalk.setTalkMemberType("2");
        complainTalk.setTalkAdmin(0);
        complainTalk.setTalkMemberId(sellerVo.getMemberId());
        complainTalk.setTalkState((byte) 1);
        int i = complainTalkMapper.insert(complainTalk);
        if(i==1){
            return "success";
        }else{
            return "failed";
        }
    }
    @RequestMapping(value="appealComplaint")
    public  ModelAndView appealComplaint(ComplainVo complain,HttpServletRequest request) throws Exception {
        StoreSellerVo sellerVo= (StoreSellerVo)request.getSession().getAttribute("sellerInfo");
        complain.setComplainHandleMemberId(sellerVo.getMemberId());
        complain.setComplainHandleDatetime(DateUtil.unixTimeStampInt().intValue());
        complain.setAppealDatetime(DateUtil.unixTimeStampInt().intValue());
        complain.setComplainState(30);
        complaintService.updateComplain(complain);
         complain =complaintService.selectByPrimaryKey(complain.getComplainId());
        ModelAndView view = new ModelAndView("complain/complaininfo","complain",complain);
        view.addObject("info",orderService.getOrderDetailById(complain.getOrderId(), complain.getAccuserId()));
        view.addObject("complainTalk", complainTalkMapper.selectByCondition(new ComplainTalk())) ;
        return view;
    }
    @RequestMapping(value="arbitreComplaint")
    public  ModelAndView arbitreComplaint(ComplainVo complain,HttpServletRequest request) throws Exception {
        complain.setComplainState(40);
        complaintService.updateComplain(complain);
        complain =complaintService.selectByPrimaryKey(complain.getComplainId());
        ModelAndView view = new ModelAndView("complain/complaininfo","complain",complain);
        view.addObject("info",orderService.getOrderDetailById(complain.getOrderId(), complain.getAccuserId()));
        view.addObject("complainTalk", complainTalkMapper.selectByCondition(new ComplainTalk())) ;
        return view;
    }

    @RequestMapping(value = "/imageUpload",method = RequestMethod.POST)
    @AuthPassport
    public @ResponseBody
    LinkedList<AlbumPic> image_upload(MultipartHttpServletRequest request){
        return super.uploadPics(request, goodsCommentPicSize, picTempPath, null);
    }
}
